import static com.googlecode.javacv.cpp.opencv_core.cvAddWeighted;
import static com.googlecode.javacv.cpp.opencv_core.cvCopy;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_COLOR;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_UNCHANGED;
import static opencvtest.OpenCVUtils.drawOnImage;
import static opencvtest.OpenCVUtils.loadAndShowOrExit;
import static opencvtest.OpenCVUtils.show;
import static opencvtest.OpenCVUtils.toIplROI;

import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;

import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

/**    
 * 文件名：JavaCvAddQiniuWater.java    
 *    
 * 版本信息：    
 * 日期：2014年3月17日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */

/**
 * @项目名称：opencv-test
 * @类名称：JavaCvAddQiniuWater
 * @类描述：
 * @创建人：zhuyi
 * @创建时间：2014年3月17日 上午10:47:07
 * @修改人：zhuyi
 * @修改时间：2014年3月17日 上午10:47:07
 * @修改备注：
 * @version
 * 
 */
public class JavaCvAddQiniuWater {

    public static void main(String[] args) {

        IplImage image = loadAndShowOrExit(new File("gogopher-ori.jpg"), CV_LOAD_IMAGE_COLOR);

        Rectangle rect = new Rectangle(330, 362, 290, 45);
        show(drawOnImage(image, rect, Color.RED), "Input template");
        IplImage roiImage = image.roi(toIplROI(rect));

        IplImage water = loadAndShowOrExit(new File("logo-2.png"), CV_LOAD_IMAGE_UNCHANGED);
        IplImage roiImage2 = IplImage.create(cvGetSize(water), roiImage.depth(), 3);
        cvCopy(roiImage, roiImage2);

        CvMat roiMat = roiImage2.asCvMat();
        CvMat waterMat = water.asCvMat();

        int n = roiMat.cols() * roiMat.rows() * roiMat.nChannels();

        for (int i = 0; i < n; i++) {
            int v = (int) roiMat.get(i);
            int wv = (int) waterMat.get(i);

            roiMat.put(i, v * 0.5 + wv * 0.5);
        }

        cvAddWeighted(roiImage2, 1.0, roiImage, 0.0, 0.0, image);
        image.roi(null);

        show(image, "add water image");

    }
}
